package com.example.app02_acticity;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private EditText etMainMessage;

    private ActivityResultLauncher<Intent> activityResultLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化布局控件对象
        etMainMessage = findViewById(R.id.et_main_message);
        Button btnMainStart1 = findViewById(R.id.btn_main_start1);
        Button btnMainStart2 = findViewById(R.id.btn_main_start2);

        // 设置按钮的监听事件
        btnMainStart1.setOnClickListener(this);
        btnMainStart2.setOnClickListener(this);

        // 使用ActivityResult API获取回调结果
        activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Log.i(TAG, "onActivityResult: ");
                if (result == null) {
                    throw new IllegalArgumentException("返回数据为空");
                }
                Intent data = result.getData();
                if (data != null && result.getResultCode() == 3) {
                    String otherMessage = data.getStringExtra("otherMessage");
                    Toast.makeText(MainActivity.this, otherMessage, Toast.LENGTH_LONG).show();
                    etMainMessage.setText(otherMessage);
                }
            }
        });
    }

    /**
     * 实现点击监听
     * @param v 发生事件的视图对象
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_main_start1) {
            // 处理启动按钮1点击事件
            Log.i(TAG, "onClick: 一般启动~~~");
            // 创建意图对象
            Intent intent = new Intent(this, OtherActivity.class);
            // 通过Intent携带数据
            String message = etMainMessage.getText().toString();
            intent.putExtra("data", message);
            // 启动一个Activity
            startActivity(intent);
        } else if (v.getId() == R.id.btn_main_start2) {
            // 处理启动按钮2点击事件
            Log.i(TAG, "onClick: 带回调启动~~~");

            // 创建意图对象
            Intent intent = new Intent(this, OtherActivity.class);
            // 通过Intent携带数据
            String message = etMainMessage.getText().toString();
            intent.putExtra("data", message);
            // 启动一个Activity，带回调
            //int reqCode = 2;
            // 方法已过时
            // 官方推荐使用ActivityResultContract实现
            // startActivityForResult(intent, reqCode);
            activityResultLauncher.launch(intent);
        }
    }

    /*
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.i(TAG, "onActivityResult: " + resultCode);

        if (requestCode == 2 && resultCode == 3) {
            // 获取data中的数据
            assert data != null;
            String otherMessage = data.getStringExtra("otherMessage");
            etMainMessage.setText(otherMessage);
        }
    } */
}